package Test;

import java.util.HashMap;

/**
 * 滑动窗口
 */
public class 无重复字符的最长子串 {
    public int lengthOfLongestSubstring(String s) {
        int left,right;
        left=right=0;
        int res=0;
        HashMap<Character, Integer> hashMap=new HashMap<>();
        while (right<s.length()){
            char c=s.charAt(right);
            int num=hashMap.getOrDefault(c,0);
            num++;
            hashMap.put(c,num);
            right++;
            while (hashMap.get(c)>1){
                char l=s.charAt(left);
                int num2=hashMap.get(l);
                num2--;
                hashMap.put(l,num2);
                left++;
            }
            res=Math.max(res,right-left);
        }
        return res;

    }
    public int lengthOfLongestSubstring1(String s) {
        int l,r;
        l=r=0;
        int res=0;
        HashMap<Character,Integer> hashMap=new HashMap<>();
        while (r<s.length()){
            char c=s.charAt(r);
            int num=hashMap.getOrDefault(c,0);
            num++;
            hashMap.put(c,num);
            while (hashMap.get(c)>1){
                char c1=s.charAt(l);
                hashMap.put(c,hashMap.getOrDefault(c,0)-1);
                l++;
            }
            res=Math.max(res,r-l);
        }
        return res;
    }
}
